﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BlogManager.Application.MultiBlogger.Controls
{
    public partial class Categories : System.Web.UI.UserControl
    {
        private int? SiteId = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["SiteId"]))
                SiteId = Convert.ToInt32(Request.QueryString["SiteId"]);

            if(!IsPostBack)
                loadData();
        }

        protected void loadData()
        {
            var categories = (from c in Data.Entities.Blog_Category.All()
                              from cp in Data.Entities.Blog_Categories_Post.All().Where(x=>x.BlogCategoryId==c.Id)
                              from p in Data.Entities.Blog_Post.All().Where(x=>x.Id==cp.BlogPostId)
                              where c.SiteId == SiteId
                              group c by new {c.Id, c.Name} into g
                              select new {g.Key.Id, g.Key.Name, PostCount = g.Count() }).ToList();

            RepeaterForCategories.DataSource = categories;
            RepeaterForCategories.DataBind();
        }
    }
}